import { async } from 'bin-code-editor'

export function dateFormat(fmt, date) {
  var ret = ''
  date = new Date(date)
  var opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString() // 秒
  }
  for (var k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
      )
    }
  }
  return fmt
}

// 检测json格式
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str)
      if (typeof obj === 'object' && obj) {
        return true
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  } else if (typeof str === 'object' && str) {
    return true
  }
}
// 字符串加密成 hex 字符串
export function sha1(s) {
  var data = new Uint8Array(encodeUTF8(s))
  var i, j, t
  var l = ((data.length + 8) >>> 6 << 4) + 16; var s = new Uint8Array(l << 2)
  s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer)
  for (t = new DataView(s.buffer), i = 0; i < l; i++)s[i] = t.getUint32(i << 2)
  s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8)
  s[l - 1] = data.length << 3
  var w = []; var f = [
    function() { return m[1] & m[2] | ~m[1] & m[3] },
    function() { return m[1] ^ m[2] ^ m[3] },
    function() { return m[1] & m[2] | m[1] & m[3] | m[2] & m[3] },
    function() { return m[1] ^ m[2] ^ m[3] }
  ]; var rol = function(n, c) { return n << c | n >>> (32 - c) }
  var k = [1518500249, 1859775393, -1894007588, -899497514]
  var m = [1732584193, -271733879, null, null, -1009589776]
  m[2] = ~m[0], m[3] = ~m[1]
  for (i = 0; i < s.length; i += 16) {
    var o = m.slice(0)
    for (j = 0; j < 80; j++) {
      w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
      t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
      m[1] = rol(m[1], 30), m.pop(), m.unshift(t)
    }
    for (j = 0; j < 5; j++)m[j] = m[j] + o[j] | 0
  }
  t = new DataView(new Uint32Array(m).buffer)
  for (var i = 0; i < 5; i++)m[i] = t.getUint32(i << 2)

  var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function(e) {
    return (e < 16 ? '0' : '') + e.toString(16)
  }).join('')
  return hex
}

function encodeUTF8(s) {
  var i; var r = []; var c; var x
  for (i = 0; i < s.length; i++) {
    if ((c = s.charCodeAt(i)) < 0x80) r.push(c)
    else if (c < 0x800) r.push(0xC0 + (c >> 6 & 0x1F), 0x80 + (c & 0x3F))
    else {
      if ((x = c ^ 0xD800) >> 10 == 0) // 对四字节UTF-16转换为Unicode
      {
        c = (x << 10) + (s.charCodeAt(++i) ^ 0xDC00) + 0x10000,
        r.push(0xF0 + (c >> 18 & 0x7), 0x80 + (c >> 12 & 0x3F))
      } else r.push(0xE0 + (c >> 12 & 0xF))
      r.push(0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F))
    }
  }
  return r
}

function getType(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1)
}
export function deepClone(source) {
  var _obj = JSON.stringify(obj) //  对象转成字符串
  var objClone = JSON.parse(_obj) //  字符串转成对象
  return objClone
}

export function completeArray(arr, targetLength, fillValue, fillStart = true) {
  const currentLength = arr.length

  if (currentLength >= targetLength) {
    return arr // 不需要补全
  }

  const missingCount = targetLength - currentLength
  const filledArray = [...arr] // 创建原数组的副本以避免修改原数组

  for (let i = 0; i < missingCount; i++) {
    if (fillStart) {
      filledArray.unshift(fillValue) // 从数组开头填充
    } else {
      filledArray.push(fillValue) // 从数组末尾填充
    }
  }

  return filledArray
}
